
// merge 1:1 ts_id using "$datapath\Eleanor\Control Groups\controlgroup", nogen keepusing(level* repeat*)



clear all
set more off, perm
capture log close

set seed 88

set maxvar 32767
//global klmChinaReach2: env klmChinaReach2
//global datapath "${klmChinaReach2}/Fuyao_Erlfang"

use "/Volumes/klmChinaReach2/Haihan/data_task_panel_cluster_May31.dta", clear
capture drop _merge
sort KIDID 


forvalues j=2/11{
	bys KIDID: egen temp`j'= max(paratel) if levell==`j' 
	bys KIDID: egen ave_pass_l`j'=max(temp`j') if levell!=.
	drop temp`j'
}
cap drop ave_pass_l_l
gen ave_pass_l_l =.
forvalues j=2/11{
	replace ave_pass_l_l=ave_pass_l`j' if levell==`j' & ave_pass_l_l ==.
}



collapse (mean) ave_pass_l_l ave_pass_l2 ave_pass_l3 ave_pass_l4 ave_pass_l5 ave_pass_l6 ave_pass_l7 ave_pass_l8 ave_pass_l9 ave_pass_l10 ave_pass_l11, by(KIDID levell)

keep if levell==1

save "rv_across_level_l.dta",replace

use "rv_across_level_l.dta", clear

//cd "C:\Users\msjin\OneDrive\Desktop\measurement\"

foreach s in l{
tempfile `s'_across_level
tempname table
postfile `table' str8(Lang) str8(quantile) str8(Category) G3 G4 G5 G6 G7 G8 G9 G10 G11 using `s'_across_level_q1, replace

forvalues i=2/10{
	local k=`i'+1
	forvalues j=`k'/11{
		count if ave_pass_l`i'>=0.75 & ave_pass_l`j'>=0.75 & ave_pass_l`i'!=. & ave_pass_l`j'!=.
		local  l_q11_`i'_`j'=`r(N)'
		count  if ave_pass_l`i'>=0.75 & ave_pass_l`j'<0.75  & ave_pass_l`j'>=0.5  & ave_pass_l`i'!=. & ave_pass_l`j'!=.
	    local l_q12_`i'_`j'=`r(N)'
		count if ave_pass_l`i'>=0.75 & ave_pass_l`j'<0.5   & ave_pass_l`j'>=0.25 & ave_pass_l`i'!=. & ave_pass_l`j'!=.
		local l_q13_`i'_`j'=`r(N)'
		count if ave_pass_l`i'>=0.75 & ave_pass_l`j'<0.25  & ave_pass_l`i'!=. & ave_pass_l`j'!=.
		local l_q14_`i'_`j'=`r(N)'
		count if ave_pass_l`i'>=0.75 & ave_pass_l`i'!=. & ave_pass_l`j'!=.
		local N_q1_`i'_`j'=`r(N)'
		forvalues mm = 1/4 {
			local f_q1`mm'_`i'_`j' = `l_q1`mm'_`i'_`j''/`N_q1_`i'_`j''
		}
		
	}
	
     if (`i'>2){
	 	forvalues jj=3/`i'{
			local l_q11_`i'_`jj'=-99
			local l_q12_`i'_`jj'=-99
			local l_q13_`i'_`jj'=-99
			local l_q14_`i'_`jj'=-99
			local N_q1_`i'_`jj'=-99
			local f_q11_`i'_`jj'=-99
			local f_q12_`i'_`jj'=-99
			local f_q13_`i'_`jj'=-99
			local f_q14_`i'_`jj'=-99
		}
	 }
	
	post `table' ("`i'") ("1") ("q11") (`l_q11_`i'_3') (`l_q11_`i'_4') (`l_q11_`i'_5') (`l_q11_`i'_6') (`l_q11_`i'_7') (`l_q11_`i'_8') (`l_q11_`i'_9') (`l_q11_`i'_10') (`l_q11_`i'_11')
	post `table' ("`i'") ("1") ("q12") (`l_q12_`i'_3') (`l_q12_`i'_4') (`l_q12_`i'_5') (`l_q12_`i'_6') (`l_q12_`i'_7') (`l_q12_`i'_8') (`l_q12_`i'_9') (`l_q12_`i'_10') (`l_q12_`i'_11')
	post `table' ("`i'") ("1") ("q13") (`l_q13_`i'_3') (`l_q13_`i'_4') (`l_q13_`i'_5') (`l_q13_`i'_6') (`l_q13_`i'_7') (`l_q13_`i'_8') (`l_q13_`i'_9') (`l_q13_`i'_10') (`l_q13_`i'_11')
	post `table' ("`i'") ("1") ("q14") (`l_q14_`i'_3') (`l_q14_`i'_4') (`l_q14_`i'_5') (`l_q14_`i'_6') (`l_q14_`i'_7') (`l_q14_`i'_8') (`l_q14_`i'_9') (`l_q14_`i'_10') (`l_q14_`i'_11')
	post `table' ("`i'") ("1") ("N")  (`N_q1_`i'_3') (`N_q1_`i'_4') (`N_q1_`i'_5') (`N_q1_`i'_6') (`N_q1_`i'_7') (`N_q1_`i'_8') (`N_q1_`i'_9') (`N_q1_`i'_10') (`N_q1_`i'_11')
	post `table' ("`i'") ("1") ("f11") (`f_q11_`i'_3') (`f_q11_`i'_4') (`f_q11_`i'_5') (`f_q11_`i'_6') (`f_q11_`i'_7') (`f_q11_`i'_8') (`f_q11_`i'_9') (`f_q11_`i'_10') (`f_q11_`i'_11')
	post `table' ("`i'") ("1") ("f12") (`f_q12_`i'_3') (`f_q12_`i'_4') (`f_q12_`i'_5') (`f_q12_`i'_6') (`f_q12_`i'_7') (`f_q12_`i'_8') (`f_q12_`i'_9') (`f_q12_`i'_10') (`f_q12_`i'_11')
	post `table' ("`i'") ("1") ("f13") (`f_q13_`i'_3') (`f_q13_`i'_4') (`f_q13_`i'_5') (`f_q13_`i'_6') (`f_q13_`i'_7') (`f_q13_`i'_8') (`f_q13_`i'_9') (`f_q13_`i'_10') (`f_q13_`i'_11')
	post `table' ("`i'") ("1") ("f14") (`f_q14_`i'_3') (`f_q14_`i'_4') (`f_q14_`i'_5') (`f_q14_`i'_6') (`f_q14_`i'_7') (`f_q14_`i'_8') (`f_q14_`i'_9') (`f_q14_`i'_10') (`f_q14_`i'_11')
}

postclose `table'

}

foreach s in l{
tempfile `s'_across_level
tempname table
postfile `table' str8(Lang) str8(quantile) str8(Category) G3 G4 G5 G6 G7 G8 G9 G10 G11 using `s'_across_level_q2, replace

forvalues i=2/10{
	local k=`i'+1
	forvalues j=`k'/11{
		count if ave_pass_l`i' < 0.75 & ave_pass_l`i' >= 0.5 & ave_pass_l`j'>=0.75 & ave_pass_l`i'!=. & ave_pass_l`j'!=.
		local  l_q21_`i'_`j'=`r(N)'
		count  if ave_pass_l`i' < 0.75 & ave_pass_l`i' >= 0.5 & ave_pass_l`j'<0.75  & ave_pass_l`j'>=0.5  & ave_pass_l`i'!=. & ave_pass_l`j'!=.
	    local l_q22_`i'_`j'=`r(N)'
		count if ave_pass_l`i' < 0.75 & ave_pass_l`i' >= 0.5 & ave_pass_l`j'<0.5   & ave_pass_l`j'>=0.25 & ave_pass_l`i'!=. & ave_pass_l`j'!=.
		local l_q23_`i'_`j'=`r(N)'
		count if ave_pass_l`i' < 0.75 & ave_pass_l`i' >= 0.5 & ave_pass_l`j'<0.25  & ave_pass_l`i'!=. & ave_pass_l`j'!=.
		local l_q24_`i'_`j'=`r(N)'
		count if ave_pass_l`i' < 0.75 & ave_pass_l`i' >= 0.5 & ave_pass_l`i'!=. & ave_pass_l`j'!=.
		local N_q2_`i'_`j'=`r(N)'
		forvalues mm = 1/4 {
			local f_q2`mm'_`i'_`j' = `l_q2`mm'_`i'_`j''/`N_q2_`i'_`j''
		}
		
	}
	
     if (`i'>2){
	 	forvalues jj=3/`i'{
			local l_q21_`i'_`jj'=-99
			local l_q22_`i'_`jj'=-99
			local l_q23_`i'_`jj'=-99
			local l_q24_`i'_`jj'=-99
			local N_q2_`i'_`jj'=-99
			local f_q21_`i'_`jj'=-99
			local f_q22_`i'_`jj'=-99
			local f_q23_`i'_`jj'=-99
			local f_q24_`i'_`jj'=-99
		}
	 }
	
	post `table' ("`i'") ("2") ("q21") (`l_q21_`i'_3') (`l_q21_`i'_4') (`l_q21_`i'_5') (`l_q21_`i'_6') (`l_q21_`i'_7') (`l_q21_`i'_8') (`l_q21_`i'_9') (`l_q21_`i'_10') (`l_q21_`i'_11')
	post `table' ("`i'") ("2") ("q22") (`l_q22_`i'_3') (`l_q22_`i'_4') (`l_q22_`i'_5') (`l_q22_`i'_6') (`l_q22_`i'_7') (`l_q22_`i'_8') (`l_q22_`i'_9') (`l_q22_`i'_10') (`l_q22_`i'_11')
	post `table' ("`i'") ("2") ("q23") (`l_q23_`i'_3') (`l_q23_`i'_4') (`l_q23_`i'_5') (`l_q23_`i'_6') (`l_q23_`i'_7') (`l_q23_`i'_8') (`l_q23_`i'_9') (`l_q23_`i'_10') (`l_q23_`i'_11')
	post `table' ("`i'") ("2") ("q24") (`l_q24_`i'_3') (`l_q24_`i'_4') (`l_q24_`i'_5') (`l_q24_`i'_6') (`l_q24_`i'_7') (`l_q24_`i'_8') (`l_q24_`i'_9') (`l_q24_`i'_10') (`l_q24_`i'_11')
	post `table' ("`i'") ("2") ("N")  (`N_q2_`i'_3') (`N_q2_`i'_4') (`N_q2_`i'_5') (`N_q2_`i'_6') (`N_q2_`i'_7') (`N_q2_`i'_8') (`N_q2_`i'_9') (`N_q2_`i'_10') (`N_q2_`i'_11')
	post `table' ("`i'") ("2") ("f21") (`f_q21_`i'_3') (`f_q21_`i'_4') (`f_q21_`i'_5') (`f_q21_`i'_6') (`f_q21_`i'_7') (`f_q21_`i'_8') (`f_q21_`i'_9') (`f_q21_`i'_10') (`f_q21_`i'_11')
	post `table' ("`i'") ("2") ("f22") (`f_q22_`i'_3') (`f_q22_`i'_4') (`f_q22_`i'_5') (`f_q22_`i'_6') (`f_q22_`i'_7') (`f_q22_`i'_8') (`f_q22_`i'_9') (`f_q22_`i'_10') (`f_q22_`i'_11')
	post `table' ("`i'") ("2") ("f23") (`f_q23_`i'_3') (`f_q23_`i'_4') (`f_q23_`i'_5') (`f_q23_`i'_6') (`f_q23_`i'_7') (`f_q23_`i'_8') (`f_q23_`i'_9') (`f_q23_`i'_10') (`f_q23_`i'_11')
	post `table' ("`i'") ("2") ("f24") (`f_q24_`i'_3') (`f_q24_`i'_4') (`f_q24_`i'_5') (`f_q24_`i'_6') (`f_q24_`i'_7') (`f_q24_`i'_8') (`f_q24_`i'_9') (`f_q24_`i'_10') (`f_q24_`i'_11')
}

postclose `table'

}

foreach s in l{
tempfile `s'_across_level
tempname table
postfile `table' str8(Lang) str8(quantile) str8(Category) G3 G4 G5 G6 G7 G8 G9 G10 G11 using `s'_across_level_q3, replace

forvalues i=2/10{
	local k=`i'+1
	forvalues j=`k'/11{
		count if ave_pass_l`i' >= 0.25 & ave_pass_l`i' < 0.5 & ave_pass_l`j'>=0.75 & ave_pass_l`i'!=. & ave_pass_l`j'!=.
		local  l_q31_`i'_`j'=`r(N)'
		count  if ave_pass_l`i' >= 0.25 & ave_pass_l`i' < 0.5 & ave_pass_l`j'<0.75  & ave_pass_l`j'>=0.5  & ave_pass_l`i'!=. & ave_pass_l`j'!=.
	    local l_q32_`i'_`j'=`r(N)'
		count if ave_pass_l`i' >= 0.25 & ave_pass_l`i' < 0.5 & ave_pass_l`j'<0.5   & ave_pass_l`j'>=0.25 & ave_pass_l`i'!=. & ave_pass_l`j'!=.
		local l_q33_`i'_`j'=`r(N)'
		count if ave_pass_l`i' >= 0.25 & ave_pass_l`i' < 0.5 & ave_pass_l`j'<0.25  & ave_pass_l`i'!=. & ave_pass_l`j'!=.
		local l_q34_`i'_`j'=`r(N)'
		count if ave_pass_l`i' >= 0.25 & ave_pass_l`i' < 0.5 & ave_pass_l`i'!=. & ave_pass_l`j'!=.
		local N_q3_`i'_`j'=`r(N)'
		forvalues mm = 1/4 {
			local f_q3`mm'_`i'_`j' = `l_q3`mm'_`i'_`j''/`N_q3_`i'_`j''
		}
		
	}
	
     if (`i'>2){
	 	forvalues jj=3/`i'{
			local l_q31_`i'_`jj'=-99
			local l_q32_`i'_`jj'=-99
			local l_q33_`i'_`jj'=-99
			local l_q34_`i'_`jj'=-99
			local N_q3_`i'_`jj'=-99
			local f_q31_`i'_`jj'=-99
			local f_q32_`i'_`jj'=-99
			local f_q33_`i'_`jj'=-99
			local f_q34_`i'_`jj'=-99
		}
	 }
	
	post `table' ("`i'") ("3") ("q31") (`l_q31_`i'_3') (`l_q31_`i'_4') (`l_q31_`i'_5') (`l_q31_`i'_6') (`l_q31_`i'_7') (`l_q31_`i'_8') (`l_q31_`i'_9') (`l_q31_`i'_10') (`l_q31_`i'_11')
	post `table' ("`i'") ("3") ("q32") (`l_q32_`i'_3') (`l_q32_`i'_4') (`l_q32_`i'_5') (`l_q32_`i'_6') (`l_q32_`i'_7') (`l_q32_`i'_8') (`l_q32_`i'_9') (`l_q32_`i'_10') (`l_q32_`i'_11')
	post `table' ("`i'") ("3") ("q33") (`l_q33_`i'_3') (`l_q33_`i'_4') (`l_q33_`i'_5') (`l_q33_`i'_6') (`l_q33_`i'_7') (`l_q33_`i'_8') (`l_q33_`i'_9') (`l_q33_`i'_10') (`l_q33_`i'_11')
	post `table' ("`i'") ("3") ("q34") (`l_q34_`i'_3') (`l_q34_`i'_4') (`l_q34_`i'_5') (`l_q34_`i'_6') (`l_q34_`i'_7') (`l_q34_`i'_8') (`l_q34_`i'_9') (`l_q34_`i'_10') (`l_q34_`i'_11')
	post `table' ("`i'") ("3") ("N")  (`N_q3_`i'_3') (`N_q3_`i'_4') (`N_q3_`i'_5') (`N_q3_`i'_6') (`N_q3_`i'_7') (`N_q3_`i'_8') (`N_q3_`i'_9') (`N_q3_`i'_10') (`N_q3_`i'_11')
	post `table' ("`i'") ("3") ("f31") (`f_q31_`i'_3') (`f_q31_`i'_4') (`f_q31_`i'_5') (`f_q31_`i'_6') (`f_q31_`i'_7') (`f_q31_`i'_8') (`f_q31_`i'_9') (`f_q31_`i'_10') (`f_q31_`i'_11')
	post `table' ("`i'") ("3") ("f32") (`f_q32_`i'_3') (`f_q32_`i'_4') (`f_q32_`i'_5') (`f_q32_`i'_6') (`f_q32_`i'_7') (`f_q32_`i'_8') (`f_q32_`i'_9') (`f_q32_`i'_10') (`f_q32_`i'_11')
	post `table' ("`i'") ("3") ("f33") (`f_q33_`i'_3') (`f_q33_`i'_4') (`f_q33_`i'_5') (`f_q33_`i'_6') (`f_q33_`i'_7') (`f_q33_`i'_8') (`f_q33_`i'_9') (`f_q33_`i'_10') (`f_q33_`i'_11')
	post `table' ("`i'") ("3") ("f34") (`f_q34_`i'_3') (`f_q34_`i'_4') (`f_q34_`i'_5') (`f_q34_`i'_6') (`f_q34_`i'_7') (`f_q34_`i'_8') (`f_q34_`i'_9') (`f_q34_`i'_10') (`f_q34_`i'_11')
}

postclose `table'

}

foreach s in l{
tempfile `s'_across_level
tempname table
postfile `table' str8(Lang) str8(quantile) str8(Category) G3 G4 G5 G6 G7 G8 G9 G10 G11 using `s'_across_level_q4, replace

forvalues i=2/10{
	local k=`i'+1
	forvalues j=`k'/11{
		count if ave_pass_l`i' < 0.25 & ave_pass_l`j'>=0.75 & ave_pass_l`i'!=. & ave_pass_l`j'!=.
		local  l_q41_`i'_`j'=`r(N)'
		count  if ave_pass_l`i' < 0.25 & ave_pass_l`j'<0.75  & ave_pass_l`j'>=0.5  & ave_pass_l`i'!=. & ave_pass_l`j'!=.
	    local l_q42_`i'_`j'=`r(N)'
		count if ave_pass_l`i' < 0.25 & ave_pass_l`j'<0.5   & ave_pass_l`j'>=0.25 & ave_pass_l`i'!=. & ave_pass_l`j'!=.
		local l_q43_`i'_`j'=`r(N)'
		count if ave_pass_l`i' < 0.25 & ave_pass_l`j'<0.25  & ave_pass_l`i'!=. & ave_pass_l`j'!=.
		local l_q44_`i'_`j'=`r(N)'
		count if ave_pass_l`i' < 0.25 & ave_pass_l`i'!=. & ave_pass_l`j'!=.
		local N_q4_`i'_`j'=`r(N)'
		forvalues mm = 1/4 {
			local f_q4`mm'_`i'_`j' = `l_q4`mm'_`i'_`j''/`N_q4_`i'_`j''
		}
		
	}
	
     if (`i'>2){
	 	forvalues jj=3/`i'{
			local l_q41_`i'_`jj'=-99
			local l_q42_`i'_`jj'=-99
			local l_q43_`i'_`jj'=-99
			local l_q44_`i'_`jj'=-99
			local N_q4_`i'_`jj'=-99
			local f_q41_`i'_`jj'=-99
			local f_q42_`i'_`jj'=-99
			local f_q43_`i'_`jj'=-99
			local f_q44_`i'_`jj'=-99
		}
	 }
	
	post `table' ("`i'") ("4") ("q41") (`l_q41_`i'_3') (`l_q41_`i'_4') (`l_q41_`i'_5') (`l_q41_`i'_6') (`l_q41_`i'_7') (`l_q41_`i'_8') (`l_q41_`i'_9') (`l_q41_`i'_10') (`l_q41_`i'_11')
	post `table' ("`i'") ("4") ("q42") (`l_q42_`i'_3') (`l_q42_`i'_4') (`l_q42_`i'_5') (`l_q42_`i'_6') (`l_q42_`i'_7') (`l_q42_`i'_8') (`l_q42_`i'_9') (`l_q42_`i'_10') (`l_q42_`i'_11')
	post `table' ("`i'") ("4") ("q43") (`l_q43_`i'_3') (`l_q43_`i'_4') (`l_q43_`i'_5') (`l_q43_`i'_6') (`l_q43_`i'_7') (`l_q43_`i'_8') (`l_q43_`i'_9') (`l_q43_`i'_10') (`l_q43_`i'_11')
	post `table' ("`i'") ("4") ("q44") (`l_q44_`i'_3') (`l_q44_`i'_4') (`l_q44_`i'_5') (`l_q44_`i'_6') (`l_q44_`i'_7') (`l_q44_`i'_8') (`l_q44_`i'_9') (`l_q44_`i'_10') (`l_q44_`i'_11')
	post `table' ("`i'") ("4") ("N")  (`N_q4_`i'_3') (`N_q4_`i'_4') (`N_q4_`i'_5') (`N_q4_`i'_6') (`N_q4_`i'_7') (`N_q4_`i'_8') (`N_q4_`i'_9') (`N_q4_`i'_10') (`N_q4_`i'_11')
	post `table' ("`i'") ("4") ("f41") (`f_q41_`i'_3') (`f_q41_`i'_4') (`f_q41_`i'_5') (`f_q41_`i'_6') (`f_q41_`i'_7') (`f_q41_`i'_8') (`f_q41_`i'_9') (`f_q41_`i'_10') (`f_q41_`i'_11')
	post `table' ("`i'") ("4") ("f42") (`f_q42_`i'_3') (`f_q42_`i'_4') (`f_q42_`i'_5') (`f_q42_`i'_6') (`f_q42_`i'_7') (`f_q42_`i'_8') (`f_q42_`i'_9') (`f_q42_`i'_10') (`f_q42_`i'_11')
	post `table' ("`i'") ("4") ("f43") (`f_q43_`i'_3') (`f_q43_`i'_4') (`f_q43_`i'_5') (`f_q43_`i'_6') (`f_q43_`i'_7') (`f_q43_`i'_8') (`f_q43_`i'_9') (`f_q43_`i'_10') (`f_q43_`i'_11')
	post `table' ("`i'") ("4") ("f44") (`f_q44_`i'_3') (`f_q44_`i'_4') (`f_q44_`i'_5') (`f_q44_`i'_6') (`f_q44_`i'_7') (`f_q44_`i'_8') (`f_q44_`i'_9') (`f_q44_`i'_10') (`f_q44_`i'_11')
}

postclose `table'

}
